<?php
declare (strict_types = 1);

namespace app\api\middleware;

use app\common\lib\Key;

class Check
{
    /**
     * 处理请求
     *
     * @param \think\Request $request
     * @param \Closure       $next
     * @return Response
     */
    public function handle($request, \Closure $next)
    {
        $token = $request->header('access-token');
        if ($token) {
            $userInfo = cache(Key::getToken($token));
            if ($userInfo) {
                $header = $request->header();
                $header['userId'] = intval($userInfo['id']);
                $header['username'] = $userInfo['username'];
                $request->withHeader($header);
            }
        }

        return $next($request);
    }
}
